package com.xiatao.serviceImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.xiatao.mapper.demoMapper;
import com.xiatao.service.AbbService;
import com.xiatao.utils.StringUtil;
@Service
public class AbbServiceImpl implements AbbService{
	@Autowired
	demoMapper demoMapper;
	@Override
	public Map<String, Object> handleAbbMsg(String params) {
		Map<String,Object> returnMap = new HashMap<>();
		returnMap.put("result", "success");
		JSONObject param=JSONObject.parseObject(params);//接收参数
		String method=StringUtil.null2empty(param.getString("method"));
		if("add".equals(method)){
			String abb = StringUtil.null2empty(param.getString("abb"));
			String mean = StringUtil.null2empty(param.getString("mean"));
			String detail = StringUtil.null2empty(param.getString("detail"));
			Map<String,Object> paramsMap = new HashMap<>();
			paramsMap.put("uuid", StringUtil.get32Uuid());
			paramsMap.put("abb", abb);
			paramsMap.put("mean", mean);
			paramsMap.put("detail", detail);
			paramsMap.put("create_time", StringUtil.getNowTime());
			demoMapper.addAbb(paramsMap);
		}else if("delete".equals(method)){
			String abb=StringUtil.null2empty(param.getString("abb"));
			demoMapper.deleteAbb(abb);
		}else if("update".equals(method)){
			String id = StringUtil.null2empty(param.getString("watchId"));
			String abb = StringUtil.null2empty(param.getString("abb"));
			String mean = StringUtil.null2empty(param.getString("mean"));
			String detail = StringUtil.null2empty(param.getString("detail"));
			Map<String,Object> paramsMap = new HashMap<>();
			paramsMap.put("id", id);
			paramsMap.put("abb", abb);
			paramsMap.put("mean", mean);
			paramsMap.put("detail", detail);
			paramsMap.put("update_time", StringUtil.getNowTime());
			demoMapper.updateAbb(paramsMap);
		}else if("query".equals(method)){
//			Map<String,Object> map = new HashMap<>();
			String msg=StringUtil.null2empty(param.getString("msg"));
			int page=Integer.parseInt(StringUtil.null2empty(param.getString("page")));
			int rows=Integer.parseInt(StringUtil.null2empty(param.getString("rows")));
			Map<String,Object> paramsMap = new HashMap<>();
			paramsMap.put("msg", msg);
			paramsMap.put("start", (page-1)*rows);
			paramsMap.put("rows", rows);
			List<Map<String,Object>> abbList = new ArrayList<>();
			abbList=demoMapper.queryAbbList(paramsMap);
			int totalNum=demoMapper.queryAbbListTotal(paramsMap);
			returnMap.put("rows", abbList);
			returnMap.put("state","1");
			returnMap.put("total", totalNum);
		}else if("watch".equals(method)||"edit".equals(method)){
			String watchCdt=StringUtil.null2empty(param.getString("watchCdt"));
			String watchId=StringUtil.null2empty(param.getString("watchId"));
			returnMap=demoMapper.watchAbbMsg(watchId);
		}
		// TODO Auto-generated method stub
		return returnMap;
	}
	@Override
	public Map<String, Object> queryNext(String params) {
		Map<String,Object> returnMap = new HashMap<>();
		JSONObject param=JSONObject.parseObject(params);//接收参数
//		String watchCdt=StringUtil.null2empty(param.getString("watchCdt"));
		String watchId=StringUtil.null2empty(param.getString("watchId"));
		returnMap=demoMapper.watchAbbNextMsg(watchId);
		if(StringUtil.isEmpty(returnMap)){
			returnMap = new HashMap<>();
			returnMap.put("result", "fail");
		}else{
			returnMap.put("result", "success");
		}
		return returnMap;
	}
	
	@Override
	public Map<String, Object> queryPro(String params) {
		Map<String,Object> returnMap = new HashMap<>();
		JSONObject param=JSONObject.parseObject(params);//接收参数
//		String watchCdt=StringUtil.null2empty(param.getString("watchCdt"));
		String watchId=StringUtil.null2empty(param.getString("watchId"));
		returnMap=demoMapper.watchAbbProMsg(watchId);
		if(StringUtil.isEmpty(returnMap)){
			returnMap = new HashMap<>();
			returnMap.put("result", "fail");
		}else{
			returnMap.put("result", "success");
		}
		return returnMap;
	}

}
